package org.tamal.struts;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import com.opensymphony.xwork2.ActionInvocation;

/**
 * This class contains some struts utility methods.
 * @author Tamal Kanti Nath
 */
final class StrutsUtils {

    private StrutsUtils() {
        // Empty
    }

    /**
     * This method checks whether an action is annotated with the specified
     * annotation.
     * @param invocation the action invocation
     * @param annotation the annotation class
     * @return true if the action is annotated
     */
    public static boolean isAnnotated(ActionInvocation invocation,
            Class<? extends Annotation> annotation) {
        Class<? extends Object> action = invocation.getAction().getClass();
        if (action.isAnnotationPresent(annotation)) {
            return true;
        }
        String methodName = invocation.getProxy().getMethod();
        if (methodName == null) {
            methodName = "execute";
        }
        boolean flag = false;
        try {
            Method method = action.getMethod(methodName);
            if (method.isAnnotationPresent(annotation)) {
                flag = true;
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
}
